$(document).ready(function () {
    /* Core JS Functions */

    /* Collapsible Panels */
    $(".mws-panel.mws-collapsible.mws-collapsed .mws-panel-body").css("display", "none");
    $(".mws-panel.mws-collapsible .mws-panel-header")
		.append("<div class=\"mws-collapse-button mws-inset\"><span></span></div>")
		.find(".mws-collapse-button span")
		.live("click", function (event) {
		    $(this)
				.parents(".mws-panel")
				.toggleClass("mws-collapsed")
				.find(".mws-panel-body")
				.slideToggle("fast");
		});

    /* Side dropdown menu */
    var checkCookie = $.cookie("navstate");
    var checkCookieSelectedNav = $.cookie("navstateactive");
    //alert(checkCookie);

    if (checkCookie != "" && checkCookie != null) {
        var arrCookies = checkCookie.split(",");

        for (var i in arrCookies) {
            $("#" + arrCookies[i]).removeClass('closed');
        }

    }
    //alert(checkCookieSelectedNav);
    $("#" + checkCookieSelectedNav).css("background-color", "#368");



    $("div#mws-navigation ul li a, div#mws-navigation ul li span")
	.bind('click', function (event) {

	    var clickId = $(this).next('ul').attr('id');

	    if (clickId !== undefined) {
	        var oldCookies = $.cookie("navstate");
	        if ($(this).next('ul').attr('class') == "closed") {
	            oldCookies += clickId + ","
	            $.cookie('navstate', oldCookies, { path: '/', expires: 10 });
	        } else {
	            //alert("Remove:" + clickId + ", from :" + oldCookies);
	            oldCookies = oldCookies.replace(clickId + ",", "");
	            $.cookie('navstate', oldCookies, { path: '/', expires: 10 });
	        }
	    } else {
	        //alert($(this).attr('id'));
	        var activenav = $(this).attr('id').replace("ahrefid", "liid");
	        $.cookie('navstateactive', activenav, { path: '/', expires: 10 });
	    }

	    if ($(this).next('ul').size() !== 0) {
	        $(this).next('ul').slideToggle('fast', function () {
	            $(this).toggleClass('closed');
	        });
	        event.preventDefault();
	    }
	});


    /* Responsive Layout Script */

    $("div#mws-navigation").live('click', function (event) {
        if (event.target === this) {
            $(this).toggleClass('toggled');
        }
    });

    /* Form Messages */

    $(".mws-form-message").live("click", function () {
        $(this).animate({ opacity: 0 }, function () {
            $(this).slideUp("medium", function () {
                $(this).css("opacity", '');
            });
        });
    });

    /* Message & Notifications Dropdown */
    $("div#mws-user-tools .mws-dropdown-menu a").click(function (event) {
        $(".mws-dropdown-menu.toggled").not($(this).parent()).removeClass("toggled");
        $(this).parent().toggleClass("toggled");
        event.preventDefault();
    });

    $('html').click(function (event) {
        if ($(event.target).parents('.mws-dropdown-menu').size() == 0) {
            $(".mws-dropdown-menu").removeClass("toggled");
        }
    });

    /* Side Menu Notification Class */
    $(".mws-nav-tooltip").addClass("mws-inset");

    /* Table Row CSS Class */
    $("table.mws-table tbody tr:even").addClass("even");
    $("table.mws-table tbody tr:odd").addClass("odd");

    /* Adding title attribute to table header, toolbar buttons and wizard navigation */
    $("table.mws-table thead tr th, .mws-panel-toolbar ul li a, .mws-panel-toolbar ul li a span, .mws-wizard ul li a, .mws-wizard ul li span").each(function () {
        $(this).attr('title', $(this).text());
    });

    /* File Input Styling */

    if ($.fn.customFileInput) {
        $("input[type='file']").customFileInput();
    }

    /* Chosen Select Box Plugin */

    if ($.fn.chosen) {
        $("select.chzn-select").chosen();
    }

    /* Tooltips */

    if ($.fn.tipsy) {
        var gravity = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'];
        for (var i in gravity)
            $(".mws-tooltip-" + gravity[i]).tipsy({ gravity: gravity[i] });

        $('input[title], select[title], textarea[title]').tipsy({ trigger: 'focus', gravity: 'w' });
    }

    /* Dual List Box */

    if ($.configureBoxes) {
        $.configureBoxes();
    }

    if ($.fn.placeholder) {
        $('[placeholder]').placeholder();
    }

    //toggler
    $(".toggler").bind("click", function (event) {
        if ($("#mws-sidebar-bg, #mws-sidebar").css("width") == "0px") {
            $("#mws-sidebar-bg, #mws-sidebar").animate({ width: "200px" }, 500);
            $("#mws-sidebar-bg, #mws-sidebar").show();
            $("#mws-container").animate({ marginLeft: "206px" }, 500);
            $("#mws-sidebar-stitch").animate({ left: "189px" }, 500, function () {
                $(window).trigger("resize");
            });
        } else {
            $("#mws-sidebar-bg, #mws-sidebar").animate({ width: "0px" }, 500);
            $("#mws-sidebar-bg, #mws-sidebar").hide();
            $("#mws-container").animate({ marginLeft: "6px" }, 500);
            $("#mws-sidebar-stitch").animate({ left: "-9px" }, 500, function () {
                $(window).trigger("resize");
            });
        }
    });
});
